package extend

import kotlinx.serialization.json.*

fun JsonObject.optInt(key: String, defaultValue: Int = 0): Int {
    return this.optIntOrNull(key) ?: defaultValue
}

fun JsonObject.optIntOrNull(key: String): Int? {
    return this[key]?.jsonPrimitive?.int
}

fun JsonObject.optLong(key: String, defaultValue: Long = 0): Long {
    return this.optLongOrNull(key) ?: defaultValue
}

fun JsonObject.optLongOrNull(key: String): Long? {
    return this[key]?.jsonPrimitive?.long
}

fun JsonObject.optFloat(key: String, defaultValue: Float = 0.0f): Float {
    return this.optFloatOrNull(key) ?: defaultValue
}

fun JsonObject.optFloatOrNull(key: String): Float? {
    return this[key]?.jsonPrimitive?.float
}


fun JsonObject.optDouble(key: String, defaultValue: Double = 0.0): Double {
    return this.optDoubleOrNull(key) ?: defaultValue
}

fun JsonObject.optDoubleOrNull(key: String): Double? {
    return this[key]?.jsonPrimitive?.double
}

fun JsonObject.optBoolean(key: String, defaultValue: Boolean = false): Boolean {
    return this.optBooleanOrNull(key) ?: defaultValue
}

fun JsonObject.optBooleanOrNull(key: String): Boolean? {
    return this[key]?.jsonPrimitive?.boolean
}

fun JsonObject.has(key: String): Boolean {
    return this[key] != null
}